package lex

import (
	"math/big"
	"showen7/lex"
	"showen7/num"
	"testing"
)

func string2BigInt(s string) *big.Int {
	bi, _ := new(big.Int).SetString(s, 10)
	return bi
}

func TestLexerNumber(t *testing.T) {
	tests := []struct {
		input    string
		expected num.Number
	}{
		{
			"123",
			num.Integer(123),
		},
		{
			"123456789012345678901234567890",
			// 创建BigInteger值
			func() num.BigInteger {
				bi := string2BigInt("123456789012345678901234567890")
				return num.BigInteger(*bi)
			}(),
		},
		{
			"1/3",
			NewRational(1, 3),
		},
		{
			"2/4",
			NewRational(1, 2),
		},
		{
			"1/123456789012345678901234567890",
			func() num.Rational {
				d, _ := new(big.Int).SetString("123456789012345678901234567890", 10)
				r := new(big.Rat).SetFrac(big.NewInt(1), d)
				return num.Rational(*r)
			}(),
		},
		{
			"0.123",
			num.Real(0.123),
		},
		{
			"0.5+0.4i",
			num.Complex(complex(0.5, 0.4)),
		},
		{
			"1/2+0.4i",
			num.Complex(complex(0.5, 0.4)),
		},
		{
			"1/2+234567890/123456789012345678901234567890i",
			num.Complex(complex(0.5, 1.899999926e-21)),
		},
	}

	for _, tt := range tests {
		l := lex.NewLexer(tt.input)
		tokens := l.ReadTokens()
		got := tokens[0].Literal.(num.Number)
		if got.String() != tt.expected.String() {
			t.Errorf("Expected %v, got %v", tt.expected, got)
		}
	}
}
